// Copyright (c) 2010 Marcos David Marín Amador <marcosmarin@gmail.com>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

function cfaqs_keyboard_posting() {
	var quickpost_text = document.getElementById("cfaqs-quickpost-text");
	
	quickpost_text.onkeyup = function(event) {
		if (event.ctrlKey && event.altKey) {
			// post message
			if (String.fromCharCode(event.keyCode) == "P") {
				document.getElementById("cfaqs-quickpost-post-button").click();
			}
			
			// preview message
			if (String.fromCharCode(event.keyCode) == "R") {
				document.getElementById("cfaqs-quickpost-preview-button").click();
			}
			
			// spellcheck message
			if (String.fromCharCode(event.keyCode) == "S") {
				document.getElementById("cfaqs-quickpost-spellcheck-button").click();
			}
			
			// reset message
			if (String.fromCharCode(event.keyCode) == "E") {
				document.getElementById("cfaqs-quickpost-reset-button").click();
			}
			
			// bold
			if (String.fromCharCode(event.keyCode) == "B") {
				document.getElementById("cfaqs-quickpost-bold").click();
			}
			
			// italics
			if (String.fromCharCode(event.keyCode) == "I") {
				document.getElementById("cfaqs-quickpost-italics").click();
			}
		}
	}
}

// setup keyboard shortcuts when in a topic
function cfaqs_keyboard_message_list(settings) {
	if (!settings.use_keyboard_shortcuts) {
		return;
	}
	
	var quickpost_text = document.getElementById("cfaqs-quickpost-text");
	
	document.addEventListener("keyup", function(event) {
		if (event.ctrlKey && event.altKey) {
			// go to quickpost form
			if (String.fromCharCode(event.keyCode) == "P") {
				location.hash = "cfaqs-quickpost-form";
				quickpost_text.focus();
			}
		}
	});
	
	cfaqs_keyboard_posting();
}

// setup keyboard shortcuts when in the topic list
function cfaqs_keyboard_topic_list(settings) {
	if (!settings.use_keyboard_shortcuts) {
		return;
	}
	
	if (settings.create_topic_behavior != "default") {
		var quickpost_text = document.getElementById("cfaqs-quickpost-text");
		
		document.addEventListener("keyup", function(event) {
			if (event.ctrlKey && event.altKey) {
				// popup quickpost form
				if (String.fromCharCode(event.keyCode) == "P") {
					$("div.board_nav div.body div.user a:nth-child(3)").click();
				}
			}
		});
		
		cfaqs_keyboard_posting();
	}
}